import { observable, action, computed } from "mobx";
import { WeaTableNew, WeaForm } from 'comsMobx';
import * as API from '../../apis/excelTemplate';
import AddExcelTemplateStore from "../dialog/addExcelTemplateStore";
import { loadSpread, releaseSpread } from "../../utils/loadSpread";
const { TableStore } = WeaTableNew;
export default class ExcelTemplateStore {
    @observable tableStore = new TableStore();
    @observable conditionForm = new WeaForm();

    @observable groups = [];
    @observable selectedGroup = '';

    @observable conditionVisible = false;

    @observable addExcelTemplateStore = new AddExcelTemplateStore();
    @computed get deleteDiabled() {
        return this.tableStore.selectedRowKeysAllPages.slice().length <= 0
    }

    @action init = () => {
        this.initLeft();
        this.initTable();
    }
    @action initLeft = () => {
        API.left(this.selectedGroup).then(this.setLeftData);
    }
    @action create = () => {
        this.addExcelTemplateStore.init({ groupNameOptions: this.groups, onSave: this.saveTemplate })
        this.addExcelTemplateStore.open();
    }
    @action edit = (id) => {
        this.addExcelTemplateStore.init({ groupNameOptions: this.groups, onSave: this.saveTemplate, id });
        this.addExcelTemplateStore.open();
    }
    @action down = (id) => {
        API.template(id).then(this.downTemplate)
    }
    @action downTemplate = (template) => {
        let fileName = template.fileName || template.name;
        if (fileName.substr(-5, 5) !== '.xlsx') {
            fileName += '.xlsx';
        }
        this.t = loadSpread(() => {
            const excelIO = new GC.Spread.Excel.IO();
            excelIO.save(template.json, (blob) => {
                saveAs(blob, fileName);
            }, function (e) {
                if(!!(window.console&&window.console.log)) {
                    window.console.log(e);
                }
            });
            releaseSpread(this.t);
        });
    }
    @action saveTemplate = (params) => {
        API.save(params).then(this.afterCreate);
    }
    @action afterCreate = () => {
        this.reloadTable();
        this.initLeft();
        this.addExcelTemplateStore.hide();
    }
    @action search = () => {
        this.initTable();
    }
    @action delete = () => {
        API.deleteTemplate(this.tableStore.selectedRowKeysAllPages.slice()).then(this.reloadTable)
    }
    @action setConditionVisible = (bool) => {
        this.conditionVisible = bool;
    }
    @action hideCondition = () => {
        this.setConditionVisible(false);
    }

    @action
    setLeftData = (data) => {
        this.groups = data;
    }
    @action changeQuickSearchValue = (value) => {
        if (this.conditionForm.isFormInit) {
            this.conditionForm.updateFields({
                groupName: { value }
            });
        }
    }

    @action changeSelectedGroup = (selectedGroup) => {
        this.changeQuickSearchValue('');
        this.selectedGroup = selectedGroup;
        this.initTable();
    }
    @action setLeftSearchValue = (value) => {
        this.searchValue = value;
        this.changeQuickSearchValue(value);
    }
    @action
    initTable = () => {
        API.splitKey({
            groupName: this.selectedGroup,
            quickSearchValue: this.quickSearchValue,
            ...this.conditionForm.getFormParams()
        }).then(this.fetchDatas);
    }
    @action
    fetchDatas = (data) => {
        this.tableStore.getDatas(data.datas);
    }
    @action reloadTable = () => {
        this.tableStore.reloadCurrent();
    }
}